<?php
namespace Clover\Model;

use Think\Model;

class AdminModel extends Model
{

    /**
     * 检测管理员登陆账号
     *
     * @param string $username
     * @param string $password
     * @return array
     */
    public function check_account($username = '', $password = '')
    {
        $arr_res = array();

        $username = trim(addslashes($username));
        $password = trim(addslashes($password));

        $password = md5($password);
        $map = array(
            "username" => $username,
            "password" => $password
        );
        $query = $this->table("dsk_admin_user")->where($map);
        if ($query->count() > 0) {
            $query = $this->table("dsk_admin_user")->where($map);
            $row = $query->find();
            //获取用户权限

            //$Role = D("Clover/Role");

            //$action_list = $Role->get_user_role($row['role_id']);

            //将用户信息写入session
            session('admin_id', $row['id']);
            session('admin_username', $row['username']);
            //session('action_list', $action_list);

            //更新用户登陆日志
            $login_info = array(
                'last_login' => mktime(),
                'last_ip' => get_client_ip()
            );

            $this->table("dsk_admin_user")->where(array('id' => $row['id']))->save($login_info);
            $arr_res = array('status' => 0, 'message' => '');
        } else {
            $arr_res = array('status' => 2, 'message' => '用户名或密码错误');
        }
        return $arr_res;
    }

    /**
     * 检测登陆状态
     *
     * @return boolean
     */
    public function get_login_status()
    {
        $is_login = FALSE;

        $admin_username = session('admin_username');

        if (!empty($admin_username)) {
            $is_login = TRUE;
        }

        return $is_login;
    }

}

?>